//
// Created by Administrator on 2020/9/27.
//

#include <stdio.h>
#include "../../lib/doctest.h"
#include "../../common/appSyncCtrl.h"
#include "../../../src/o5gcn/include/cnApi.h"
#include "../../../src/o5gran/include/ranApi.h"
#include "../../../src/o5gue/include/ueApi.h"
#include "../../common/commDef.h"
#include "appInit.h"

void UT_AppInit(void)
{
    int32_t rc;
    int argc;
    char* argv[4];

    argc = 3;

    UT_AppSyncInit();

    if (UT_AppSyncIsXxApp(APP_TYPE_CN))
    {
        argv[0] = (char*)"CN_APP";
        argv[1] = (char*)"--app-type=APP_TYPE_CN";
        argv[2] = (char*)"--config=" O5G_RAN_CONFIG_FILE_PATH_4_TEST "gnb.band78.tm1.106PRB.usrpb210.toml";

        rc = CN_Init(argc, argv);
        REQUIRE(O5G_RC_OK == rc);
    }
    if (UT_AppSyncIsXxApp(APP_TYPE_RAN))
    {
        argv[0] = (char*)"RAN_APP";
        argv[1] = (char*)"--app-type=APP_TYPE_RAN";
        argv[2] = (char*)"--config=" O5G_RAN_CONFIG_FILE_PATH_4_TEST "gnb.band78.tm1.106PRB.usrpb210.toml";

        rc = RAN_Init(argc, argv);
        REQUIRE(O5G_RC_OK == rc);
    }
    if (UT_AppSyncIsXxApp(APP_TYPE_UE))
    {
        argv[0] = (char*)"UE_APP";
        argv[1] = (char*)"--app-type=APP_TYPE_UE";
        argv[2] = (char*)"--config=" O5G_RAN_CONFIG_FILE_PATH_4_TEST "ue.band78.tm1.106PRB.usrpb210.toml";

        rc = UE_Init(argc, argv);
        REQUIRE(O5G_RC_OK == rc);
    }

    UT_AppSyncOnce();

    if (UT_AppSyncIsXxApp(APP_TYPE_CN))
    {
        rc = CN_Start();
        REQUIRE(O5G_RC_OK == rc);
    }
    if (UT_AppSyncIsXxApp(APP_TYPE_RAN))
    {
        rc = RAN_Start();
        REQUIRE(O5G_RC_OK == rc);
    }
    if (UT_AppSyncIsXxApp(APP_TYPE_UE))
    {
        rc = UE_Start();
        REQUIRE(O5G_RC_OK == rc);
    }
}

void UT_AppFinal(void)
{
    int32_t rc;

    UT_AppSyncOnce();

    if (UT_AppSyncIsXxApp(APP_TYPE_CN))
    {
        rc = CN_Stop();
        REQUIRE(O5G_RC_OK == rc);
        rc = CN_Final();
        REQUIRE(O5G_RC_OK == rc);
    }
    if (UT_AppSyncIsXxApp(APP_TYPE_RAN))
    {
        rc = RAN_Stop();
        REQUIRE(O5G_RC_OK == rc);
        rc = RAN_Final();
        REQUIRE(O5G_RC_OK == rc);
    }
    if (UT_AppSyncIsXxApp(APP_TYPE_UE))
    {
        rc = UE_Stop();
        REQUIRE(O5G_RC_OK == rc);
        rc = UE_Final();
        REQUIRE(O5G_RC_OK == rc);
    }

    UT_AppSyncFinal();
}